package com.itmk.set_course.controller;

import com.alibaba.fastjson.JSONObject;
import com.itmk.course.entity.Course;
import com.itmk.course.entity.CourseParm;
import com.itmk.course.entity.CourseVo;
import com.itmk.course.service.CourseService;
import com.itmk.course.service.impl.CourseServiceImpl;
import com.itmk.course_teacher.entity.CourseTeacher;
import com.itmk.course_teacher.service.CourseTeacherService;
import com.itmk.course_teacher.service.impl.CourseTeacherServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

///setCourse.do的请求，都会被该servlet处理
@WebServlet("/setCourse.do")
public class SetCourseServlet extends HttpServlet {
    private static final String PREX = "WEB-INF/jsp/admin";
    private CourseService courseService = new CourseServiceImpl();
    private CourseTeacherService courseTeacherService = new CourseTeacherServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if(method.equals("setCourseUI")){
            setCourseUI(req,resp);
        }else if(method.equals("list")){
            list(req,resp);
        }else if(method.equals("setTeacher")){
            setTeacher(req,resp);
        }else if(method.equals("setTeacherSave")){
            setTeacherSave(req,resp);
        }
    }

    private void setTeacherSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        String teacherId = req.getParameter("teacherId");
        String courseId = req.getParameter("courseId");
        //封装参数
        CourseTeacher courseTeacher = new CourseTeacher();
        courseTeacher.setTeacherId(teacherId);
        courseTeacher.setCourseId(courseId);
        //调用servie
        courseTeacherService.add(courseTeacher);
        //返回成功信息
        //组装前端数据格式
        JSONObject object = new JSONObject();
        object.put("code", 200);
        object.put("msg", "分配成功");
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void setTeacher(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String teacherId = req.getParameter("teacherId");
        req.setAttribute("teacherId",teacherId);
        req.getRequestDispatcher(PREX+"/setCourseTeacher.jsp").forward(req,resp);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        int page = Integer.valueOf(req.getParameter("page"));
        int limit = Integer.valueOf(req.getParameter("limit"));
        String courseName = req.getParameter("courseName");
        //封装参数
        CourseParm parm = new CourseParm();
        parm.setCourseName(courseName);
        parm.setLimit(limit);
        parm.setPage(page);
        //调用service
        List<CourseVo> list = courseService.getList(parm);
        //查询总条数
        Integer count = courseService.getCourseCount(parm);
        //组装前端数据格式
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "查询成功");
        object.put("data", list);
        object.put("count", count);
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }
    /**
     * 返回授课管理页面
     * @param req
     * @param resp
     */
    private void setCourseUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PREX+"/setCourse.jsp").forward(req,resp);
    }
}
